package whf.framework.tools.generator;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import whf.framework.entity.AbstractEntity;
import whf.framework.tools.generator.cfg.ConfigFactory;
import whf.framework.util.ParamUtils;
import whf.framework.web.struts.WebDispatchAction;

/**
 * @author wanghaifeng
 *
 */
public class DispatchActionGenerator extends AbstractGenerator{

	static String[] types = {"ActionMapping","BaseBO",
			"HttpServletRequest","HttpServletResponse"};
	static String[] args = {"mapping","baseForm","request","response"};
	static String[] methods = {"doInit","doCreate","doList","doView",
		"doUpdate","doDelete"};
	
	private String iserviceClassName;
	
	public DispatchActionGenerator(String packageName,String className,String iserviceClassName){
		super.setPackageName(packageName+".web");
		super.setClassName(className);
		this.iserviceClassName = iserviceClassName;
	}
	
	protected void gen() {
		super.setSuperClassName(WebDispatchAction.class.getName());
		super.addImport(List.class.getName());
		super.addImport(HttpServletRequest.class.getName());
		super.addImport(HttpServletResponse.class.getName());
		super.addImport(ParamUtils.class.getName());
		super.addImport(AbstractEntity.class.getName());
		super.addMethod("public","service",iserviceClassName,null,null,null,"return ("+iserviceClassName+")super.getService();");
		for(int i=0;i< methods.length;i++){
			String body = ConfigFactory.getInstance().getTemplateContent("DispatchActionGenerator."+methods[i]);
			super.addMethod("protected",methods[i],"ActionForward",types,
					args,new Class[]{Exception.class},body);
		}
	}

}
